package com.citycloud.datac.client.controller;

import java.net.ConnectException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.citycloud.datac.client.client.DataCOauth2Client;
import com.citycloud.datac.client.dto.DetailedUserInfo;
import com.citycloud.datac.client.resoult.ResultObject;

@RestController
public class TestController {
	@Autowired
	DataCOauth2Client oauth2Client;

	/**
	 * 使用用户名和密码获取token
	 * 
	 * @return
	 * @throws ConnectException
	 */
	@RequestMapping(path = { "/getTokenByUserName" }, method = { RequestMethod.GET, RequestMethod.POST })
	public String test() throws ConnectException {
		// 密码为输入密码MD5加密的值
		ResultObject map = oauth2Client.getToken("yqcs4", "21218cca77804d2ba1922c33e0151105");
		return "this is a test!";
	}

	/**
	 * 使用code码获取token
	 * 
	 * @param code
	 * @return
	 * @throws ConnectException
	 */
	@RequestMapping(path = { "/getTokenByCode" }, method = { RequestMethod.GET, RequestMethod.POST })
	public String test2(String code) {
		ResultObject map = oauth2Client.getToken(code);
		System.out.println(map);
		return "this is a test!";
	}

	/**
	 * 根据token获取用户信息
	 * 
	 * @param access_token
	 * @return
	 * @throws ConnectException
	 */
	@RequestMapping(path = { "/getUserInfo" }, method = { RequestMethod.GET, RequestMethod.POST })
	public String test3(String access_token) {
		DetailedUserInfo map = oauth2Client.getUserInfo(access_token);
		System.out.println(map);
		return "this is a test!";
	}

	/**
	 * check_token
	 * 
	 * @param access_token
	 * @return
	 * @throws ConnectException
	 */
	@RequestMapping(path = { "/check_token" }, method = { RequestMethod.GET, RequestMethod.POST })
	public String test4(String access_token) {
		Map<String, Object> map = oauth2Client.checkToken(access_token);
		System.out.println(map);
		return "this is a test!";
	}

}
